1   package com.iluwatar;
2   
3   import java.io.Serializable;
4   
5   /**
6    * The Initialize-on-demand-holder idiom is a secure way of 
7    * creating lazy initialize singleton Object in Java.
8    * refer to "The CERT Oracle Secure Coding Standard for Java"
9    * By Dhruv Mohindra, Robert C. Seacord p.378
10   * 
11   * Singleton objects usually are heavy to create and sometimes need to serialize them.
12   * This class also shows how to preserve singleton in Serialized version of singleton.
13   * 
14   * @author mortezaadi@gmail.com
15   *
16   */
17  public class InitializingOnDemandHolderIdiom implements Serializable{
18  
19  	private static final long serialVersionUID = 1L;
20  
21  	private static class HelperHolder {
22  		public static final InitializingOnDemandHolderIdiom INSTANCE = new InitializingOnDemandHolderIdiom();
23  	}
24  
25  	public static InitializingOnDemandHolderIdiom getInstance() {
26  		return HelperHolder.INSTANCE;
27  	}
28  
29  	private InitializingOnDemandHolderIdiom() {
30  	}
31  
32  	protected Object readResolve() {
33  		return getInstance();
34  	}
35  
36  }